package com.example.login.servlets;

import com.example.login.utils.DBUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;

@WebServlet(urlPatterns = "/delete")
public class DeleteServlets extends HttpServlet {
    Connection connection=null;
    PreparedStatement preparedStatement=null;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        try {
            connection = DBUtil.getConnection();
            String sql="delete from sys_user where id=?";
            preparedStatement= connection.prepareStatement(sql);
            preparedStatement.setString(1,id);
            int i = preparedStatement.executeUpdate();
            System.out.println("影响了"+i+"行");
            resp.sendRedirect(req.getContextPath()+"/list.jsp");

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if(preparedStatement!=null){
                   preparedStatement.close();
                }
                if(connection!=null){
                    connection.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }

    }}
